import csv
import os
import numpy as np
from data.fixed_data_process import processLabelledData
import pandas as pd
from random import shuffle

# 遍历文件夹及其子文件夹中的文件，并存储在一个列表中
# 输入文件夹路径、空文件列表[]
# 返回 文件列表Filelist,包含文件名（完整路径）

def get_filelist(dir,Filelist):
    newDir = dir
    if os.path.isfile(dir):
        Filelist.append(dir)
    elif os.path.isdir(dir):
        for s in os.listdir(dir):
            newDir = os.path.join(dir, s)
            get_filelist(newDir,Filelist)
    return Filelist
#整合userid和passworddata在一个csv中，第一列为userid后面的数据
#对应该用户键入的password'data
def concate_userid_passworddata(useridfilename,passworddatafilename,dir):

    useridData = []
    passwordData = []

    os.chdir(dir)
    with open(useridfilename, 'r') as useridfile:
        useridDataRow = csv.reader(useridfile, delimiter=',', lineterminator='\n')
        for rawRow in useridDataRow:
            useridData.append(rawRow)

    with open(passworddatafilename, 'r') as passworddatafile:
        passwordDataRow = csv.reader(passworddatafile, delimiter=',', lineterminator='\n')
        for rawRow in passwordDataRow:
            passwordData.append(rawRow)
    for i in range(len(passwordData)):
        useridData[i].extend(passwordData[i])

    labelledData = useridData
    return labelledData

def divideLabelledData(labelledData,validId,dataRowSize = 8):
    standardData = []
    index = 0
    for list in labelledData:
        userid = list[0]
        i = 1
        while(i < len(list) - dataRowSize):
            standardData.append([])
            if userid == validId:
                standardData[index].append(1)
            else:
                standardData[index].append(0)
            standardData[index].extend(list[i:i+dataRowSize-1])
            i = i + (int)(dataRowSize/2)
            index = index + 1
    return standardData
def buildData(validId,dir ,useridfilename, passworddatafilename, dataRowSize = 8) -> None:
    try:
        os.remove(dir+'/processed-invalid-data.csv')
        os.remove(dir+'/processed-valid-data.csv')
    except: pass
    # 数据处理：将原始数据集划分为 目标valid用户和非目标invalid用户 （目标用户：id为userid的用户数据）
    # labelledData为处理后的数据

    labelledData = concate_userid_passworddata(useridfilename, passworddatafilename, dir)
    standardData = divideLabelledData(labelledData, validId, dataRowSize)  # 将不定长的数据转化成定长的标准数据 以便用于网络训练
    # 数据划分：
    # 数据值的相关处理后，将目标用户和非目标用户的数据 划分为两个文件
    processLabelledData(standardData,dir+'/processed-valid-data.csv',dir+'/processed-invalid-data.csv')







